<template>
    <!-- 添加或修改菜单对话框 -->
    <el-dialog :title="!form.id ? '新增' : '修改'"
               :close-on-click-modal="false"
               :visible.sync="visible">
        <el-form v-if="visible" ref="dataForm" :model="form" :rules="rules" size="small" label-width="80px">
            <el-form-item label="应用" prop="name">
                <el-select v-model="form.name" placeholder="请选择应用">
                    <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="服务器" prop="ip">
                <el-select v-model="form.ip" placeholder="请选择服务器">
                    <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button size="small" @click="visible = false">取 消</el-button>
            <el-button size="small" type="primary" @click="dataFormSubmit">确 定</el-button>
        </div>
    </el-dialog>
</template>

<script>
    import {getDict} from '~/utils'
    import {addObj, getDivision, getObj, putObj,getObjByCode} from '@/admin/api/division'

    export default {
        name: "serverDeployAdd",
        data() {
            return {
                // 遮罩层
                loading: true,
                // 是否显示弹出层
                visible: false,
                form: {
                    name: '',
                    id:'',
                    ip: '',
                    port: '',
                    divisionCode: '',
                    hasChildren:true
                },
                // 表单校验
                rules: {
                    name: [
                        {required: true, message: "名称不能为空", trigger: "blur"}
                    ],
                    ip: [
                        {required: true, message: "IP不能为空", trigger: "blur"}
                    ]
                },
                options: [],
            }
        },
        created(){

        },
        methods: {
            loadOptions({ action, parentNode, callback, instanceId }){
                this.data = [];
                getDivision({lazy:true,parentId:parentNode.divisionCode}).then(response => {
                    this.data = response.data.data
                    this.data.map(el=>{
                    for(let key in el){
                        el.id= el.divisionCode,
                        el.label= el.divisionName
                        }
                    })
                    parentNode.children = this.data
                    callback()
                })

            },

            init(isEdit, data) {

                this.options = getDict('DIVISION_LEVEL');

                this.visible = true
                this.$nextTick(() => {
                    if (isEdit) {
                        getObj(data.id).then(response => {
                            let res = response.data
                            this.form = {
                                parentId: res.data.parentCode,
                                id: res.data.divisionId,
                                divisionName: res.data.divisionName,
                                divisionLevel: res.data.divisionLevel,
                                divisionCode: res.data.divisionCode,
                                hasChildren: true,
                            }

                        })
                    } else {
                        this.form = {
                            parentId: this.form.parentId,
                            id:'',
                            divisionName: '',
                            divisionLevel: '',
                            divisionCode: '',
                            hasChildren:true
                        }
                    }
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        if (this.form.parentId === undefined) {
                            this.form.parentCode = '000000'
                        }else{
                            this.form.parentCode = this.form.parentId
                        }
                        if (this.form.id) {
                            this.form.divisionId = this.form.id
                            putObj(this.form).then(data => {
                                this.$message.success('修改成功')
                                this.visible = false
                                this.$emit('refreshDataList')
                            })
                        } else {
                            addObj(this.form).then(data => {
                                this.$message.success('添加成功')
                                this.visible = false
                                this.$emit('refreshDataList')
                            })
                        }
                    }
                })
            },
           
        }
    }
</script>
